 <?php
 use Lib\PasswordHash;
 use Think\Cache;
 
	 /*
	 *会员登录检查
	 *
	 */
	function _UserLogin($table,$where,$data){
		
/* 		if ($_SESSION['boboid']) {
			//已经登录，无需重新登录
			return 4;
		} */
		$field = 'username';
		if (is_array($where)) {
			$list = M($table)->where($where)->field($field)->select();
		} else {
			$list = M($table)->where($where,$data)->field($field)->select();
		}
		if ($list) {
			return 1;	
		} else {
			return 0;
		}			 
	 }
	 /*
	 *会员登录
	 *
	 */
	function _goLogin($table,$where,$data){
		
/* 		if ($_SESSION['boboid']) {
			//已经登录，无需重新登录
			return 4;
		} */
			
		if (is_array($where)) {
			$list = M($table)->where($where)->find();
		} else {
			$list = M($table)->where($where,$data)->find();
		}
		if ($list) {
			//S('user',$list);
			return $list;	
		} else {
			return 0;
		}			 
	 }	 
	/*
	 *判断是否登录
	 */
	function error_dump(){
		header("Content-Type: text/html; charset=UTF-8");
		
		if (!session('boboid')) {
			outClear();
			if (session('user_id')) {
				session(null);
			}			
		}		 
	 }	

	 /*
	 *查询一条数据
	 *
	 */
	function _getOne($table,$where,$data,$cache='',$field='*'){
		
		    error_dump();
			if (S($cache) && $cache) {
				$list = S($cache,$list) ;
			} else {			
				if (is_array($where)) {
					$list = M($table)->where($where)->field($field)->find();
				} else {
					$list = M($table)->where($where,$data)->field($field)->find();
				}
				//dump($cache);die;
				if ($cache) {
					S($cache,$list) ;
				}				
			}
		
			return $list ;					 
	 }	
	 
	/*
	 *分页
	 *%d - double,包含正负号的十进制数（负数、0、正数）
	 *%s - string,字符串
	 *%f - float,浮点数
	 *$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();
	 */
	function Page($table,$where,$data='',$num,$join='',$join2='',$join3=''){
		
		$User = M($table); // 实例化User对象

		if (is_array($where)) {
			$count = $User->join($join)->join($join2)->join($join3)->where($where)->count();// 查询满足要求的总记录数
		} else {
			$count = $User->join($join)->join($join2)->join($join3)->where($where,$data)->count();// 查询满足要求的总记录数
		}
		
		$Page       = new \Think\Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数

		$Page->setConfig('header', '');
		$Page->setConfig('prev', '上一页');
		$Page->setConfig('next', '下一页');
		$Page->setConfig('last', '末页');
		$Page->setConfig('first', '首页');
		$Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
		$Page->lastSuffix = false;//最后一页不显示为总页数
		$Page->parameter;
		$show	= $Page->BOBOshow();// 分页显示输出
		//dump($Page->parameter);die;
		
		return $show ;			
		 
	 }
	 

	//分页输出函数 (默认每頁顯示的記錄數为10)
	function getpage($count, $pagesize = 10) {
		$p = new Think\Page($count, $pagesize);
		// $p->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
		$p->setConfig('prev', '上一页');
		$p->setConfig('next', '下一页');
		$p->setConfig('last', '末页');
		$p->setConfig('first', '首页');
		$p->setConfig('first', '首页');
		// $p->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%');
		$p->lastSuffix = false;//最后一页不显示为总页数
		$p->rollPage = 3;//分页栏每页显示的页数
		
		return $p;
	} 
 
	/*
	 *强制刷新
	 */
	function _Refresh($get,$cache){
		
		if ($get) {
			S('clear',$get,600);	
		}
		if (S('clear')) {
			S($cache,null);
		}		 
	 }		 
	 
	/*
	 *超时强制退出
	 */
	function timeOver(){
		
		$over =30*60;//30分钟后强制退出
		$time = time();
		
		$getTime = $_SESSION['timeOver'];	
		$nowT = $time - $getTime ;
		
		if ($nowT>$over && $_SESSION['timeOver']) {
			session(null);	
			return 0;
		} elseif($nowT<$over && $_SESSION['timeOver']) {
			session('timeOver',time());
			return 1;
		} else {
			return 0;
		}
	 }	 

	/*
	 *商品发布成功超时强制退出
	 */
	function goods_timeOver(){
		
		$over =10*60;//30分钟后强制退出
		$time = time();
		
		$getTime = $_SESSION['ok_timeOver'];	
		$nowT = $time - $getTime ;
		
		if ($nowT>$over && $_SESSION['ok_timeOver']) {
			session('ok_timeOver',null);
			session('st_goods',null);		
			return 0;
		} else {
			return 0;
		}
	 }	 
	
